package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.TutorDAORemote;
import proyecto.EntityBeans.Tutor;

@Stateless
@Remote(TutorBLRemote.class)
public class TutorBL implements TutorBLRemote {

	TutorDAORemote tutorDAORemote;
	
	public int save(Tutor tutor) {	
		
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return tutorDAORemote.save(tutor); //alta de tutor		
		
	}
		
	public Tutor findById(int id) {
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return tutorDAORemote.findById(id);
		
	}
	
		
	public void merge(Tutor tutor) {
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
			tutorDAORemote.merge(tutor);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
		
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return tutorDAORemote.findAll();
	}
	
	
	public List findAllNoBaja() {		
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return tutorDAORemote.findAllNoBaja();
	}
	
	
	public List findAllBaja() {		
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return tutorDAORemote.findAllBaja();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return tutorDAORemote.findByIdList(id);
	}
	
	
	public List findByNombreApellidoDni(String nombre, String apellido, int dni)
	{	     
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return tutorDAORemote.findByNombreApellidoDni(nombre, apellido, dni);
	}
	
		
	public void remove(int idTutor){	
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		tutorDAORemote.remove(idTutor);
	}
	
	public void refresh(Tutor tutor){
		try
		{
			Context context = new InitialContext();
			tutorDAORemote = (TutorDAORemote) context.lookup("TutorDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		tutorDAORemote.refresh(tutor);
	}
}
